package com.petmarket.controller;

import com.alibaba.fastjson.JSON;
import com.petmarket.client.LoginClient;
import com.petmarket.client.PetClient;
import com.petmarket.client.TransferClient;
import com.petmarket.client.UserClient;
import com.petmarket.common.PageResult;
import com.petmarket.common.Result;
import com.petmarket.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.fisco.bcos.sdk.jni.common.JniException;
import org.fisco.bcos.sdk.v3.codec.ContractCodecException;
import org.fisco.bcos.sdk.v3.transaction.model.exception.ContractException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;


/**
 * 用户controller类
 */
@RestController
@RequestMapping("/User")
@Slf4j
public class UserController {
    @Autowired
    private UserClient userClient;

    @Autowired
    private PetClient petClient;

    @Autowired
    private LoginClient loginClient;

    @Autowired
    private TransferClient transferClient;

    @PostMapping("/query")
    public Result<User> query(@RequestBody User user) {
        try{
            if (log.isInfoEnabled()) {
                log.info("UserController.query:{}", JSON.toJSONString(user));
            }
            User resUser =  userClient.query(user);
            return Result.ok(resUser);
        }
        catch(Exception e){
            return Result.fail("查询失败");
        }
    }


    @PostMapping("/insert")
    public Result<Boolean> insert(@RequestBody User user) {
        try{
            if (log.isInfoEnabled()) {
                log.info("UserController.insert:{}", JSON.toJSONString(user));
            }
            userClient.insert(user);
            return Result.ok();
        }
        catch(Exception e){
            return Result.fail("开户失败");
        }
    }


    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody User user) {
        try{
            if (log.isInfoEnabled()) {
                log.info("UserController.update:{}", JSON.toJSONString(user));
            }
            User queried = userClient.query(user);
            String amount=user.getBalance();
            String balance= queried.getBalance();
            //int amountInt=amount.
            int amountInt = Integer.parseInt(amount);
            int balanceInt=Integer.parseInt(balance);

            int balanceRes=balanceInt+amountInt;
            String finalBalance=Integer.toString(balanceRes);

            queried.setBalance(finalBalance);
            userClient.edit(queried);
            return Result.ok();
        }
        catch (Exception e){
            return Result.fail("修改失败");
        }
    }

    @PostMapping("/remove")
    public Result remove(@RequestBody User user) {
        try{
            if (log.isInfoEnabled()) {
                log.info("UserController.remove:{}", JSON.toJSONString(user));
            }
            userClient.remove(user);
            return Result.ok();
        }
        catch(Exception e){
            return Result.fail("删除失败");
        }
    }

    @PostMapping("/transfer")
    public Result transfer(@RequestBody TransferRequest transferRequest) throws ContractException, ContractCodecException, JniException {

        Pet pet=new Pet();
        pet.setId(transferRequest.getPetId());

        Pet queried = petClient.query(pet);

        String price= queried.getPetPrice();
        BigInteger amount =new BigInteger(price);

        LoginDetail loginDetail=new LoginDetail();
        loginDetail.setId(transferRequest.getBuyerId());
        LoginDetail detail = loginClient.query(loginDetail);
        userClient.transfer(transferRequest.getBuyerId(),transferRequest.getSellerId(),amount,detail.getPrivateKey());

        Transfer transfer=new Transfer();
        transfer.setBuyerId(transferRequest.getBuyerId().toString());
        transfer.setSellerId(transferRequest.getSellerId().toString());
        transfer.setAmount(amount.toString());
        transfer.setIsDeleted("0");
        transferClient.insert(transfer);

        return Result.ok();
    }



    @PostMapping("/getUserPage")
    public Result<PageResult<User>> getUserPage(@RequestBody User user){
        try{
            if (log.isInfoEnabled()){
                log.info("UserController.getUserPage:{}", JSON.toJSONString(user));
            }
            PageResult<User> result = userClient.getUserPage(user.getPageNo(),user.getPageSize());
            return Result.ok(result);
        }
        catch (Exception e){
            return Result.fail("获取列表失败");
        }
    }
}
